package com.gxa.saas.admin.config;

import com.gxa.saas.core.common.JsonResult;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @Author cowboy
 * @Date 2022/7/25 16:02
 * @Version 1.0
 */

public class UploadConfig {


//    @RequestMapping(value = "/upload",method = {RequestMethod.GET,RequestMethod.POST})
//    public Map upload(@RequestParam("file")MultipartFile file,@RequestParam(value = "type",required = false)String type) throws IOException {
//        JsonResult jsonResult = new JsonResult();
//
//        if (file.isEmpty()) {
//            jsonResult.setCode("0");
//            jsonResult.setMsg("不能上传空文件");
//        }else if (file.getSize()>1024*1024){
//            jsonResult.setCode("0");
//            jsonResult.setMsg("文件大小不能超过1M");
//        }else {
//            //获取文件名
//            String filename = file.getOriginalFilename();
//            //获取文件名后缀(带.)
//            String suffix = filename.substring(filename.lastIndexOf(".") + 1);
//            if ("jpg".equals(suffix) || "jpeg".equals(suffix) || "png".equals(suffix) || "gif".equals(suffix)){
//                //生成UUID名称
//                String uuid = UUID.randomUUID().toString();
//                String uuidName = uuid.replaceAll("-", "");
//                //上传到服务器/usr目录下的/upload目录下
//                if ("goods".equals(type))
//                    uuidName = "goods/"+uuidName;
//                if("receipt".equals(type))
//                    uuidName = "receipt/"+uuidName;
//                File dest = new File("/usr/upload/" + uuidName + "." + suffix);
//                file.transferTo(dest);
//
//                // 把生成的文件名返给前端
//                jsonResult.setCode("1");
//                jsonResult.set("fileName",uuidName+"."+suffix);
//            }else {
//                jsonResult.setCode("0");
//                jsonResult.setMsg("只能上传图片格式");
//            }
//        }
//        return jsonResult.getMap();
//    }
public static String upload(MultipartFile picture) throws IOException {
//        String path = "http://121.5.226.127:8080/a/";
    String path = "http://43.142.113.212:8088/uploadfiles/";

//为上传到服务器的文件取名，使用UUID防止文件名重复
//        String filename= UUID.randomUUID().toString()+"-"+myfile.getOriginalFilename();
    String type = picture.getOriginalFilename().substring(picture.getOriginalFilename().lastIndexOf("."));
    String filename= UUID.randomUUID().toString()+type;

//使用Jersey客户端上传文件
    Client client = Client.create();
    WebResource webResource = client.resource(path +"/" + URLEncoder.encode(filename,"utf-8"));
    webResource.put(picture.getBytes());
    System.out.println("上传成功");
    return path+filename;
}
}
